package cn.iocoder.yudao.module.contract.service.basicData;

import cn.iocoder.yudao.module.contract.dal.dataobject.basicData.BasicProductCodeDO;
import cn.iocoder.yudao.module.contract.dal.dataobject.basicData.BasicSupplierDO;
import cn.iocoder.yudao.module.contract.dal.mysql.basicData.BasicSupplierMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.annotations.VisibleForTesting;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.contract.controller.admin.basicData.vo.*;
import cn.iocoder.yudao.module.contract.dal.dataobject.basicData.BasicVarietyDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.contract.dal.mysql.basicData.BasicVarietyMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.contract.enums.ErrorCodeConstants.*;

/**
 * 品种海关编码 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class BasicVarietyServiceImpl extends ServiceImpl<BasicVarietyMapper, BasicVarietyDO> implements BasicVarietyService {

    @Resource
    private BasicVarietyMapper basicVarietyMapper;

    @Override
    public Long createBasicVariety(BasicVarietySaveReqVO createReqVO) {
        // 插入
        BasicVarietyDO basicVariety = BeanUtils.toBean(createReqVO, BasicVarietyDO.class);
        validateUnique(basicVariety);
        basicVarietyMapper.insert(basicVariety);
        // 返回
        return basicVariety.getId();
    }

    @Override
    public void updateBasicVariety(BasicVarietySaveReqVO updateReqVO) {
        // 校验存在
        validateBasicVarietyExists(updateReqVO.getId());
        // 更新
        BasicVarietyDO updateObj = BeanUtils.toBean(updateReqVO, BasicVarietyDO.class);
        validateUnique(updateObj);
        basicVarietyMapper.updateById(updateObj);
    }

    @Override
    public void deleteBasicVariety(Long id) {
        // 校验存在
        validateBasicVarietyExists(id);
        // 删除
        basicVarietyMapper.deleteById(id);
    }

    private void validateBasicVarietyExists(Long id) {
        if (basicVarietyMapper.selectById(id) == null) {
            throw exception(BASIC_VARIETY_NOT_EXISTS);
        }
    }
    @VisibleForTesting
    void validateUnique(BasicVarietyDO data) {
        long count = basicVarietyMapper.selectCount(new LambdaQueryWrapper<BasicVarietyDO>().eq(BasicVarietyDO::getName,data.getName()).ne(data.getId()!=null,BasicVarietyDO::getId,data.getId()));
        // 如果 id 为空，说明不用比较是否为相同 id 的用户
        if (count>0 ) {
            throw exception("品种已存在");
        }
    }

    @Override
    public BasicVarietyDO getBasicVariety(Long id) {
        return basicVarietyMapper.selectById(id);
    }

    @Override
    public PageResult<BasicVarietyDO> getBasicVarietyPage(BasicVarietyPageReqVO pageReqVO) {
        return basicVarietyMapper.selectPage(pageReqVO);
    }

}